package com.example.apidocserver.infrastructure.util.entity;

import com.example.apidocserver.domain.document.entity.ApiParamField;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Field;
import java.util.Comparator;
import java.util.List;

/**
 * 复制子类对象
 *
 * @author xyqlg
 */
public class EntityCommonUtil {

    /**
     * 将子类属性复制给父类
     *
     * @param parent   父类对象
     * @param children 子类对象
     */
    public static <T, E> void childrenToParent(T parent, E children) {
        try {
            if (parent.getClass().isAssignableFrom(children.getClass())) {
                // 获取子类属性名称和值，将其设置到父类对象中
                Field[] fields = parent.getClass().getDeclaredFields();
                for (Field field : fields) {
                    field.setAccessible(true);
                    Object value = field.get(children);
                    field.set(parent, value);
                }
            }
        } catch (IllegalAccessException exception) {
            exception.printStackTrace();
        }
    }
}